(function(w) {
    w.app=w.app || {};
    function mapline(el, adrs,cpt,starttitle,cartitle) {
        $('body').append('<div id="idmaphide"></div>')
        var map = new BMap.Map(el || "allmap"); // 创建Map实例
        map.enableScrollWheelZoom(true); //开启鼠标滚轮缩放
        var top_right_navigation = new BMap.NavigationControl({ anchor: BMAP_ANCHOR_TOP_RIGHT, type: BMAP_NAVIGATION_CONTROL_SMALL });
        map.addControl(top_right_navigation);
        
        getadds(adrs || ['重庆', '北京大兴瑞海二区'], function(arr) {
            line(arr.shift(), arr,cpt,cartitle);
        });

        function line(pt, pts,cpt,cartitle) {
            var p = pt.split(',')[0],
                t = pt.split(',')[1];
            _p = pts[0].split(',')[0];
            _t = pts[0].split(',')[1];
            var p1 = new BMap.Point(p,t);
            var p2 = new BMap.Point(_p,_t);
            _cp = cpt.split(',')[0];
            _ct = cpt.split(',')[1];
            var jp=new BMap.Point(_cp || 116.37811,_ct || 39.851817);

            addicon(p, t, 'images/mp2_03.png', 34, 44,18,55);
            addicon(_p, _t, 'images/mp2_06.png', 34, 44,18,55);
            addspoticon(p,t);
            addspoticon(_p,_t);
            addtext(p, t, starttitle ||'[总仓]');
            var labeltip=addtexts(_cp,_ct,cartitle)

            //map.addOverlay(new BMap.Marker(jp));
            var car=addicon(_cp,_ct,'images/car.png',57,31,40,30);
            car.addEventListener('click',function(){
                if(!labeltip.show){
                    map.addOverlay(labeltip);
                }else{
                    map.removeOverlay(labeltip);
                }
                labeltip.show=!labeltip.show;
            })
            
            var driving = new BMap.DrivingRoute(map)
            
            driving.setSearchCompleteCallback(function(results){
                searchComplete(results);
                var pts = driving.getResults().getPlan(0).getRoute(0).getPath();    //通过驾车实例，获得一系列点的数组
                var polyline = new BMap.Polyline(pts,{
                    strokeColor: "#4fa1a4",
                    strokeWeight: 6,
                    strokeOpacity: 0.8
                });     
                map.addOverlay(polyline);
            })
            map.centerAndZoom(new BMap.Point(_cp, _ct), 12);

            driving.search(p1, jp);
            driving.search(jp, p2);

            function searchComplete(results){
                if (driving.getStatus() != BMAP_STATUS_SUCCESS){
                    return ;
                }
                var plan = results.getPlan(0);
                var output='';
                output += plan.getDuration(true) + "\n";                //获取时间
                output += "总路程为：" ;
                output += plan.getDistance(true) + "\n";  
                console.log(output);
            }
        }


        function addtext(p, t, text) {
            var opts = {
                position: new BMap.Point(p, t),
                offset: new BMap.Size(20, -20)
            }
            var label = new BMap.Label(text, opts);
            label.setStyle({
                color: "#fff",
                fontSize: "14px",
                lineHeight: "20px",
                border: '0px',
                padding: '4px',
                background: 'rgba(0,0,0,0.3)'
            });
            map.addOverlay(label);
        }   
        function addtexts(p,t,text){
            var opts = {
                position: new BMap.Point(p, t),
                offset: new BMap.Size(20, -20)
            }
            var label = new BMap.Label(text, opts);
            label.setStyle({
                color: "#fff",
                fontSize: "14px",
                lineHeight: "20px",
                border: '0px',
                padding: '4px',
                background: 'rgba(0,89,130,0.5)'
            });
            label.show=0;
            //map.addOverlay(label);
            return label;
        }

        //添加icon
        function addicon(p, t, img, w, h,_l,_t) {
            var pt = new BMap.Point(p, t);
            var icon = new BMap.Icon(img, new BMap.Size(w, h));
            icon.setAnchor(new BMap.Size(_l, _t))
            var marker = new BMap.Marker(pt, { icon: icon })
            map.addOverlay(marker);
            return marker;
        }
        function addspoticon(p,t){
            var pt = new BMap.Point(p, t);
            var icon1 = new BMap.Icon('images/mp2_11.png', new BMap.Size(20, 20));
            icon1.setAnchor(new BMap.Size(10, 10))
            var marker1 = new BMap.Marker(pt, { icon: icon1 })
            map.addOverlay(marker1);
        }
        //获取pt
        function getadds(val, call) {
            var idmaphide = new BMap.Map("idmaphide");
            idmaphide.centerAndZoom(new BMap.Point(116.404, 39.915), 11);
            var localSearch = new BMap.LocalSearch(idmaphide);
            var arr = [];
            var index = -1;
            localSearch.enableAutoViewport();
            localSearch.setSearchCompleteCallback(function(searchResult) {　　　　
                var poi = searchResult.getPoi(0);　　　　 //console.log(poi.point.lng + "," + poi.point.lat);   
                arr.push(poi.point.lng + "," + poi.point.lat)
                if (arr.length == val.length) {
                    call(arr);
                } else {
                    ua();
                }　　
            });
            function ua() {
                if (index < val.length - 1) {
                    index++;
                    localSearch.search(val[index])
                }
            }
            ua();
        }
    }
    w.app.maplines = mapline;
}(window));